<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">


            <div style="margin: 10px 10px 10px 10px;display: flex;justify-content: space-between">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">词汇名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="search" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <!--<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>-->
                        </div>
                    </div>
                </form>
                <form class="layui-form layui-form-pane" onsubmit="return addSensitive()">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">添加词汇</label>
                            <div class="layui-input-inline">
                                <input type="text" id="addwords" autocomplete="off" class="layui-input" placeholder="请输入词汇，回车键提交">
                            </div>
                        </div>

                    </div>
                </form>
            </div>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
{include file="public/footer" /}
<script>
    var global_table;
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate=layui.laydate;
             global_table = table;
            table.render({
            elem: '#currentTableId',
            url: '/admin/Imdata/sensitive_word_index?action=ajax',
            method: 'post',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data, //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'words', title: '词汇名称',edit:'text'},
                {field: 'as_often', title: '拦截次数', sort: true},
                {field: 'use_num', title: '使用人数',sort:true,event:'usenum'},
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    searchParams: data.field
                }
            }, 'data');

            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加活动',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/roulette_add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'LAYTABLE_TIPS') {
                alert('自定义的,不用的话可以将该方法删除')
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            $.ajax({
                url: "/admin/Imdata/sensitive_word_write?action=ajax",
                type: 'post',
                async: true,
                data: {
                    value:value,
                    id:data.id,
                    field:field
                },
                dataType: 'json',
                success: function (res) {
                    if(res.status){
                        return;
                    }else{
                        layer.msg(res.msg);
                    }
                }
            });
        });
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '编辑活动',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/roulette_write?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if(obj.event === 'check'){
                var index = layer.open({
                    title: '查看详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/roulette_static?active_id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }else if (obj.event === 'usenum'){
                var index = layer.open({
                    title: '查看使用玩家',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Imdata/sensitive_use_index?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

    });

    /**
     * @context 添加敏感词
     * @returns {boolean}
     */
    function addSensitive()
    {
        let words=$("#addwords").val();
        if(words!=""&&words!=undefined){
            $.ajax({
                url: "/admin/Imdata/sensitive_word_add?action=ajax",
                type: 'post',
                async: true,
                data: {
                    words
                },
                dataType: 'json',
                success: function (res) {
                    if(res.status){
                        console.log(global_table);
                        global_table.reload('currentTableId', {
                            page: {
                                curr: 1
                            },
                        }, 'data');
                        return;
                    }else{
                        layer.msg(res.msg);
                    }
                }
            });
        }
        $("#addwords").val('');
        return false;
    }
</script>

</body>
</html>